<template>
  <div>
    <!-- 分页器 -->
    <el-pagination
      background
      layout="sizes, prev, pager, next, jumper"
      :page-sizes="[5,8,10]"
      :total="total"
      @current-change="PageIndexClick"
      @size-change="PageSizeClick"
    ></el-pagination>

    <!-- 表格 -->
    <el-table v-if="postList.length > 0" :data="postList" style="width: 1000px">
      <el-table-column label="日期" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.create_date.split('T')[0] }}</span>
        </template>
      </el-table-column>

      <el-table-column label="名字" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.user.nickname }}</span>
        </template>
      </el-table-column>

      <el-table-column label="标题" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.title }}</span>
        </template>
      </el-table-column>

      <el-table-column label="照片" width="180">
        <template slot-scope="scope">
          <!-- <img class="cover_img"    :src="scope.row.cover[0].url" alt /> -->
          <img class="cover_img"  v-if="scope.row.cover && scope.row.cover.length > 0"   :src="scope.row.cover[0].url" alt />
     
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini">
            <div @click="$router.push('./sendPost?id='+scope.row.id)">编辑</div>
          </el-button>
          <el-button size="mini" type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
export default {
  data() {
    return {
      postList: [],
      pageIndex: 1,
      pageSize: 10,
      total: 10
    };
  },
  methods: {
    get_post() {
      this.$axios({
        method: "get",
        url: "/post",
        params: {
          pageIndex: this.pageIndex,
          pageSize: this.pageSize
        }
      }).then(res => {
        const { data, total } = res.data;
        this.total = total;
        // 判断url
        data.forEach(item => {
          item.cover.forEach((item2, index) => {
            if (item2.url.indexOf("bucket") < 1) {
              item2.url = this.$axios.defaults.baseURL + item2.url;
            }
          });
        });
        this.postList = data;
      });
    },
    PageIndexClick(index) {
      this.pageIndex = index;
      this.get_post();
      // console.log("this.pageIndex: ", this.pageIndex);
    },
    PageSizeClick(size) {
      this.pageSize = size;
      this.get_post();
      // console.log("size: ", size);
    },
  },
  mounted() {
    this.get_post();
  }
};
</script>

<style lang="less" scoped>
.cover_img {
  width: 80px;
  height: 80px;
  object-fit: cover;
}
</style>